package myPack;

import java.io.File;
import java.io.Serializable;
import java.security.MessageDigest;
import java.util.HashMap;

import com.sun.java.swing.plaf.windows.resources.windows;


public class SongList implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HashMap<String, Song> map;
	private MessageDigest md;
	
	
	public SongList() {
		this.map = new HashMap<>();
		try {
			this.md = MessageDigest.getInstance("MD5");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public String getMD5(Object data) {
		byte[] hash = md.digest((byte[]) data) ;
		 
		 StringBuffer sb = new StringBuffer();
		 for (int i = 0; i < hash.length; ++i) {
		     sb.append(
		         Integer.toHexString(
		             (hash[i] & 0xFF) | 0x100
		         ).toLowerCase().substring(1,3)
		     );
		 }
		 return sb.toString();
	}
	
	
	public void addSong(Song song) {
		String hash = getMD5(song);
		if (this.map.containsKey(hash)) {
			System.out.println("A file with this md5-value has already been added.");
			return;
		} else {
			this.map.put(hash, song);
			
		}
	}
	
	public void addFolder(File folder) {
		for (String filename: folder.list()) {
			File file = new File(filename);
			if (file.isDirectory()) {
				addFolder(file);
			} else {
				Song song = new Song(file);
				this.addSong(song);
			}
		}
	}
	
}
